package chapter1.classTest.task.superMarket;

import java.util.Scanner;
import java.io.*;

/*
实现之前的超市(名称和地址属性)会员积分系统,有如下的基本功能(实现开卡和查询即可)，会员有卡号和积分两个属性：
积分累计
积分兑换
查询剩余积分
修改密码
开卡
退出
,采用序列化的方式,每次启动如果没有超市对象,则创建新对象;否则从序列化的文件中读取,退出时保存所有数据。后续处理方式相同.
static ,超市类中。

 */
public class TestMarket {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Supermarket a = download(); //读取序列化对象


        int choice = 0;
        while (true) {
            System.out.println("1:加新会员，2:查看超市和会员信息，3:给指定用户涨分，4:兑换礼品，5:获得获得超市会员人数，6:返回积分值最大的会员，7:查询会员积分，9:退出");
            System.out.print("请选择:");
            choice = input.nextInt();
            if (choice == 1) {
                System.out.println("--加新会员--");
                Member b = new Member();
                System.out.print("请输入新会员姓名:");
                b.setName(input.next());
                System.out.print("请输入新会员号:");   //开卡，会员号就是卡号
                b.setCardNo(input.next());
                System.out.print("请输入新会员的积分:");
                b.setJifen(input.nextInt());
                System.out.print("请输入新会员手机号:");
                b.setPhone(input.next());
                if (a.add(b)) {
                    System.out.println("新会员添加成功");
                } else {
                    System.out.println("新会员添加失败");
                }
            } else if (choice == 2) {
                System.out.println("--查看超市信息--");
                a.show();
            } else if (choice == 3) {
                System.out.println("--给指定用户涨分--");
                System.out.print("请输入会员号:");
                String cardNo = input.next();
                System.out.print("请输入要涨多少积分:");
                int addjifen = input.nextInt();
                boolean c = a.inc(cardNo, addjifen);
                if (c) {
                    System.out.println("涨分成功");
                } else {
                    System.out.println("账号不存在");
                }
            } else if (choice == 4) {
                System.out.println("--兑换礼品--");
                System.out.print("请输入会员号:");
                String cardNo = input.next();
                System.out.print("请输入要兑换的礼品类型序号:");
                int i = input.nextInt();
                System.out.println("兑换的结果是" + a.duiHuan(cardNo, i));
            } else if (choice == 5) {
                System.out.println("--获得超市会员人数--");
                System.out.println("超市会员人数为" + a.getRenShu());
            } else if (choice == 6) {
                System.out.println("--返回积分值最大的会员--");
                Member b = a.getBiggestJifen();
                if (b == null) {
                    System.out.println("会员数组为空，没有最大积分的会员");
                } else {
                    System.out.println("积分值最大的会员是" + b.getName());
                }
            } else if (choice == 7) {
                System.out.println("--获得会员积分--");
                System.out.print("请输入会员号：");
                int jifen = a.getJiFen(input.next());
                if (jifen > 0)
                    System.out.println("该会员积分为：" + jifen);
                else
                    System.out.println("获取积分失败");
            } else if (choice == 9) {
                System.out.println("退出");
                upload(a);  //写入序列化对象
                break;
            }
        }

    }


    public static Supermarket download() { //下载超市对象
        Supermarket s = null;
        ObjectInputStream in = null;
        try {
            in = new ObjectInputStream(new FileInputStream("F:\\123\\1.txt"));
            s = (Supermarket) in.readObject();
        } catch (Exception e) {
            s = new Supermarket("世纪华联", "南京东路170号");
            System.out.println("出错");
            e.printStackTrace();
            System.out.println(e.getMessage());
        } finally {
            try {
                if (in != null)
                    in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return s;

    }


    public static void upload(Supermarket sup) {    //上传超市对象
        ObjectOutputStream out = null;
        try {
            out = new ObjectOutputStream(new FileOutputStream("F:\\123\\1.txt"));
            out.writeObject(sup);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null)
                    out.close();
            } catch (IOException e) {
            }
        }
    }


}
